/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.manager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.atjava.jcrt.JcrtSession;
import com.atjava.jcrt.JcrtSessionFactory;
import com.atjava.jcrt.core.exception.CoreException;
import com.atjava.jcrweb.Constants;
import com.atjava.jcrweb.JcrWebException;
import com.atjava.jcrweb.info.UserInfo;

/**
 * DirectoryManager
 * @author biggie
 *
 */
public class UserManager {

	private static UserManager manager = null;
	private JcrtSessionFactory factory = null;
	private JcrtSession jsession = null;
    /**
     * Log4j
     */
    private Logger log = Logger.getLogger(this.getClass().getName());
    
	public static UserManager getInstance() throws JcrWebException{
		if(manager == null){
			manager = new UserManager();
		}
		return manager;
	}
	
	private UserManager() throws JcrWebException{
		try {
			factory = JcrtSessionFactory.getInstance();
			jsession = factory.openJcrtSession();
		} catch (CoreException e) {
			log.error("DirectoryManager",e);
			throw new JcrWebException(e);
		}
	}
	

	public void createUser(UserInfo userInfo) throws  JcrWebException{
		try {
			synchronized(this){
				Calendar c = Calendar.getInstance();
				c.setTimeInMillis(System.currentTimeMillis());
				userInfo.setDatetime(c);	
				jsession.save(userInfo, "/"+userInfo.getUserCode());
			}
		} catch (CoreException e) {
			log.error("createUser("+userInfo+")",e);
			throw new JcrWebException(e);
		} catch (Exception e){
			log.error("createUser("+userInfo+")",e);
			throw new JcrWebException(e);
		}
	}
	public UserInfo getUser(String usercode) throws  JcrWebException{
		Object obj;
		try {
			obj = jsession.load(UserInfo.class, "/"+usercode);
		} catch (CoreException e) {
			log.error("getUser("+usercode+")",e);
			throw new JcrWebException(e);
		}
		if(obj!=null){
			return (UserInfo)obj;
		}
		return null;
	}
	public List getAllUsers() throws JcrWebException{
		List list = null;
		try {
			List result = jsession.getChildren(UserInfo.class, "/");
			if(result!=null){
				list = new ArrayList();
				
    			for(int i=result.size();i>0;i--){
    				list.add(result.get(i-1));
    			}
			}
		} catch (CoreException e) {
			log.error("getAllUsers()",e);
			throw new JcrWebException(e);
		} catch (Exception e){
			log.error("getAllUsers()",e);
			throw new JcrWebException(e);
		}
		return list;
	}
	public void updateUser(UserInfo userInfo) throws  JcrWebException{
		try {
			synchronized(this){
				Calendar c = Calendar.getInstance();
				c.setTimeInMillis(System.currentTimeMillis());
				userInfo.setDatetime(c);	
				jsession.save(userInfo, "/"+userInfo.getUserCode());
			}
		} catch (CoreException e) {
			log.error("updateUser("+userInfo+")",e);
			throw new JcrWebException(e);
		} catch (Exception e){
			log.error("updateUser("+userInfo+")",e);
			throw new JcrWebException(e);
		}
	}
	
	public void deleteUser(String usercode) throws  JcrWebException{
		try {
			jsession.delete(UserInfo.class, "/"+usercode);
		} catch (CoreException e) {
			log.error("deleteUser("+usercode+")",e);
			throw new JcrWebException(e);
		} catch (Exception e){
			log.error("deleteUser("+usercode+")",e);
			throw new JcrWebException(e);
		}
	}
	public UserInfo getLoginUserinfo(HttpSession session){
		return (UserInfo)session.getAttribute(Constants.LOGIN_USER_SESSION_INFO);
	}

}
